<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountlog_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    function arrays($key){
    	$data = array(
    		'var_user_loan' 			=> array("0"=>"投资客", "1"=>"借款户"),
			'var_account_deduct_type' 	=> array(SCENE_AC=>'现场认证费用', VOUCH_ADVANCED=>'担保垫付扣费',
    											 BORROW_KOUHUI=>'借款人罚金扣回', AC_OTHER=>'其它扣费'),
	        'account_type' 			=> array(0=>'全部',
					TENDER=>'投标',RECHARGE=>'用户充值',CASH_CANCEL=>'取消提现解冻',CASH_FROST=>'提现冻结',
					INVEST=>'扣除冻结款',FEE=>'充值手续费',
					AWARD_ADD=>'投标奖励',INVEST_FALSE=>'投标失败资金返回',RECHARGE_FEE=>'提现手续费',COLLECTION=>'待收金额',
					INVEST_REPAYMENT=>'投资人回款',RECHARGE_SUCCESS=>'提现审核通过',
					RECHARGE_FALSE=>'提现失败',SYSTEM_REPAYMENT=>'逾期系统还款',LATE_RATE=>'逾期利息扣除',TICHENG=>'投标提成',
					LATE_COLLECTION=>'逾期收入',AC_OTHER=>'其它扣费',OTHER_FEE=>'其他费用',INVITE_REWARD=>'推荐奖励',
					AWARD_MINUS=>'扣除投标奖励', INVEST_SINGLE_FALSE=>'用户撤标',
	        		RECHARGE_CHINABANK_REWARD=>'网银在线充值奖励',INVEST_CANCEL_ACC=>'撤销投标本金返回',INVEST_CANCEL_AWARD=>'撤销投标奖励扣除',INVEST_FANXIAN=>'投标返现奖励',REDPACKAGE=>'代金券',REDPACKAGE_DEDUCT=>'消费代金券'),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    
	function add($user_id, $data = array()){
		$user_id = intval($user_id);
		if($user_id > 0){
			$mode = substr(strval($user_id),-1,1);
			$this->db->insert('rsh_account_log_'.$mode, $data);
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
	
	function get_accountlog_today_list($data){
	
		$_select = "l.id, l.user_id, l.ftype, l.total, l.money, l.use_money, l.no_use_money, 
					l.collection, l.to_user, l.remark, l.add_time,l.op,u.username, u.realname, u.is_loan";
		
		$start = strtotime(date('Y-m-d 00:00:00'));
		$end = time();
		$_where = " where 1=1 ";
		$_where .=" and l.add_time >= ".$start." and l.add_time <= ".$end;
		
		if(isset($data['where']['ftype']) && $data['where']['ftype']){
			$_where .= " and l.ftype = ".$data['where']['ftype'];
		}
		if(isset($data['where']['user_id']) && $data['where']['user_id']){
			$_where .= " and l.user_id = ".$data['where']['user_id'];
		}

		$accountlog = array();
		for($i = 0; $i<10; $i++){
			$sql_result = "select $_select from rsh_account_log_{$i} as l left join rsh_user as u on l.user_id=u.user_id";
			$sql_result .= $_where;
			$query = $this->db->query($sql_result);
			$accountloglist = $query->result_array();
			
			$accountlog = array_merge($accountlog, $accountloglist);
		}	
		$vdata['accountlog_list'] =$accountlog;
		return $vdata;
	}
    
	

	function get_accountlog_list($data){
		
		$table = "rsh_account_log";
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != 0){
			$mode = substr(strval($data['where']['user_id']),-1,1);
			$table = $table . "_" . $mode;
		}
		//封装查询内容
		$_select = "l.id, l.user_id, l.ftype, l.total, l.money, l.use_money, l.no_use_money, 
					l.collection, l.to_user, l.remark, l.add_time,l.op,u.username, u.realname, u.is_loan";
		
		//封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['ftype']) && $data['where']['ftype']){
			$_where .= " and l.ftype = ".$data['where']['ftype'];
		}
		if(isset($data['where']['user_id']) && $data['where']['user_id']){
			$_where .= " and l.user_id = ".$data['where']['user_id'];
		}
		if(isset($data['where']['strtime']) && $data['where']['strtime']){
			$strtime = strtotime($data['where']['strtime'].' 00:00:00');
			$_where .= " and l.add_time >= ".$strtime;
		}
		if(isset($data['where']['endtime']) && $data['where']['endtime']){
			$endtime = strtotime($data['where']['endtime'].' 23:59:59');
			$_where .= " and l.add_time <= ".$endtime;
		}
		$_order = " order by add_time desc,id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		//封装sql
		$sql_result = "select $_select from {$table} as l left join rsh_user as u on l.user_id=u.user_id";
		$sql_result .= $_where;
		$sql_result .= $_order;
		if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql_result);
			$vdata['accountlog_list'] = $query->result_array();
			return $vdata;
		}else{
			$sql_result .= $_limit;	
			$query = $this->db->query($sql_result);
			$data['accountlog_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from {$table} as l";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['accountlog_count'] = $query->row()->count;
		return $data;
	}
    
	function get_accountlogall_list($data){
		//封装查询内容
		$_select = "l.id, l.user_id, l.ftype, l.total, l.money, l.use_money, l.no_use_money, 
					l.collection, l.to_user, l.remark, l.add_time,u.username, u.realname, u.is_loan";
		
		//封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['ftype']) && $data['where']['ftype'] != 0){
			$_where .= " and l.ftype = ".$data['where']['ftype'];
		}
		if(isset($data['where']['utype']) && $data['where']['utype'] != -1){
			$_where .= " and u.is_loan = ".$data['where']['utype'];
		}
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != 0){
			$_where .= " and l.user_id = ".$data['where']['user_id'];
		}
		if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
			$strtime = strtotime($data['where']['strtime'].' 00:00:00');
			$_where .= " and l.add_time >= ".$strtime;
		}
		if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
			$endtime = strtotime($data['where']['endtime'].' 23:59:59');
			$_where .= " and l.add_time <= ".$endtime;
		}
		$_order = " order by add_time desc, id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		//封装sql
		$sql_result = "select $_select from rsh_account_log as l left join rsh_user as u on l.user_id=u.user_id";
		$sql_result .= $_where;
		$sql_result .= $_order;
		if(isset($data['limit']) && $data['limit'] == 'all'){
			/*$db_slave = $this->load->database('slave', TRUE); 
			$query = $db_slave->query($sql_result);*/
			$query = $this->db->query($sql_result);
			$data['accountlog_list'] = $query->result_array();
			return $data;
		}else{
			$sql_result .= $_limit;	
			/*$db_slave = $this->load->database('slave', TRUE); 
			$query = $db_slave->query($sql_result);*/
			$query = $this->db->query($sql_result);
			$data['accountlog_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_account_log as l left join rsh_user as u on l.user_id=u.user_id ";
		$sql_count .= $_where;
		/*$db_slave = $this->load->database('slave', TRUE); 
		$query = $db_slave->query($sql_count);*/
		$query = $this->db->query($sql_count);
		$data['accountlog_count'] = $query->row()->count;
		return $data;
	}
	
	function cash_fee($user_id){
		$last_time = strtotime("-14 day", time());
		$mf_money = 0;//可免费提现的金额
		$mode = substr(strval($user_id),-1,1);
		$sql = " SELECT use_money FROM `rsh_account_log_$mode` WHERE user_id={$user_id} AND `add_time` < {$last_time} ORDER BY id DESC LIMIT 1 ";
		$query = $this->db->query($sql);
		$result = $query->row_array();
		$mf_money = isset($result['use_money']) ? $result['use_money'] : 0;
		//15天前可用金额都可免费提现
		/*$cz_money = 0;//15天金额记录
		$tx_money = 0;
		$fu = 0;
		$sql = " SELECT * FROM `rsh_account_log_$mode` WHERE user_id={$user_id} AND `add_time`> {$last_time} ORDER BY id ASC ";
		$query = $this->db->query($sql);
		$list = $query->result_array();
		if($list){
			foreach($list as $val){
				if($val['ftype'] == RECHARGE){//充值
					if($val['remark']=='平台充值成功'){
						$mf_money = $mf_money + $val['money'];//平台充值、奖励可以免费提现
					}else{
						$cz_money = $cz_money + $val['money'];
					}
				}elseif($val['ftype'] == TENDER){//投标
					$cz_money = $cz_money - $val['money'];
					if($cz_money < 0){
						//若15天内充值小于使用，则降低可免费提现金额，并重新计算15天内金额记录
						$mf_money = $mf_money + $cz_money;
						$cz_money = 0;
					}
				}elseif($val['ftype'] == INVEST_FALSE){
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == VIP_REPAY){//VIP费用返回
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == AWARD_ADD){//投标奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == BORROW_SUCCESS){//借款
					$mf_money = $mf_money + $val['money'];//借款可以免费提现
				}elseif($val['ftype'] == MARGIN){//借款保证金冻结
					$mf_money = $mf_money - $val['money'];
				}elseif($val['ftype'] == CREDITOR_SUCCESS){//出售债权成功入账
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == BORROW_FEE){//借款手续费
					$mf_money = $mf_money - $val['money'];
				}elseif($val['ftype'] == CASH_FROST){//提现
					if($mf_money < $val['money']){
						$fu = $val['money'] - $mf_money;
					}
					$tx_money = $tx_money + $val['money'];
					$mf_money = $mf_money - $val['money'];
				}elseif($val['ftype'] == CASH_CANCEL){//提现取消
					if($tx_money >= $val['money']){
						$tx_money = $tx_money - $val['money'];
						$mf_money = $mf_money + $val['money'];
						if($fu){
							$mf_money = $mf_money - $fu;
							$fu = $fu -$fu;
						}
					}
				}elseif($val['ftype'] == RECHARGE_FALSE){//提现失败
					if($tx_money >= $val['money']){
						$tx_money = $tx_money - $val['money'];
						$mf_money = $mf_money + $val['money'];
						if($fu){
							$mf_money = $mf_money - $fu;
							$fu = $fu -$fu;
						}
					}
					
				}elseif($val['ftype'] == REPAYMENT){//还款
					$cz_money = $cz_money - $val['money'];
					if($cz_money < 0){
						//若15天内充值小于使用，则降低可免费提现金额，并重新计算15天内金额记录
						$mf_money = $mf_money + $cz_money;
						$cz_money = 0;
					}
				}elseif($val['ftype'] == RECHARGE_REWARD){//线下充值奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == INVITE_REWARD){//线下充值奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == RECHARGE_CHINABANK_REWARD){//线下充值奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == LIMIT_REWARD){//日排名奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == REPAYMENT_TENDER_REWARD){//回款再投标奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == CHOUJIANG_REWARD){//转盘抽奖奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == INVITE_REWARD){//推荐奖励
					$mf_money = $mf_money + $val['money'];
				}elseif($val['ftype'] == INVEST_REPAYMENT){//收到还款
					//$mf_money = $mf_money + $val['money'];
					$str = $val['remark'];
					$arr = explode('obj/',$str);
					if(count($arr)>1){
						$borrow_id = substr($arr[1],0,4);
					}else{
						$arr = explode('.html',$str);
						$arr2 = explode('/a',$arr[0]);
						$borrow_id = $arr2[1];
					}
					$borrow_id = $borrow_id ? intval($borrow_id) : 0;
					//$arr2 = explode('/a',$arr[0]);
					//$borrow_id = $arr2[1];
					$sql = " select ftype from `rsh_borrow` where id={$borrow_id} limit 1";
					$query = $this->db->query($sql);
					$tmpbo = $query->row_array();
					if($tmpbo && $tmpbo['ftype'] == 5){
						$cz_money = $cz_money + $val['money'];
					}else{
						$mf_money = $mf_money + $val['money'];
					}
				}
				$mf_money = max($mf_money,0);
				$mf_money = min($val['use_money'], $mf_money);
			}
		}*/
		return $mf_money;
	}
	
	/**
	 * 用户操作金额分类汇总
	 */
	function get_sum_group($user_id){
		$table = 'rsh_account_log';
		$mode = substr(strval($user_id),-1,1);
		$table = $table . "_" . $mode;
		$sql = "select ftype,sum(money) as total_money from `{$table}` where user_id='{$user_id}' group by ftype";
		$query = $this->db->query($sql);
		$result = $query->result_array();
		$list = array();
		if($result){
			foreach($result as $v){
				$list[$v['ftype']] = $v['total_money'];
			}
		}
		return $list;
	}
	

	/**
	 * 用户操作金额分类汇总
	 */
	function get_sum_type($user_id, $ftype){
		$table = 'rsh_account_log';
		$mode = substr(strval($user_id),-1,1);
		$table = $table . "_" . $mode;
		$sql = "select sum(money) as total_money from `{$table}` where user_id='{$user_id}' and ftype={$ftype}";
		//限制本月消费代金卷的用户 begin tangc 
		if($ftype == REDPACKAGE_DEDUCT){
			$start = strtotime(date("Y-m-01 00:00:00"));
			$end = date("Y-m-01 23:59:59");
			$end = strtotime("$end +1 month -1 day");
			$sql = $sql." and add_time >= ".$start." and add_time <= ".$end;
		}
		//end
		$query = $this->db->query($sql);
		$result = $query->row_array();
		if($result){
			return $result['total_money']?$result['total_money']:0.00;	
		}
		return 0;
		
	}
	
	
	/**
	 * 15天内不可提现金额 RECHARGE,REDPACKAGE_DEDUCT,INVEST_FANXIAN,INVITE_REWARD
	 */
	function donnot_mention_15($user_id){
		
			$table = 'rsh_account_log';
			$mode = substr(strval($user_id),-1,1);
			$table = $table . "_" . $mode;
			
			$weekend=time()-15*24*3600;
			
			$_where = " where 1=1 ";
			$_where .= " and ftype in(". RECHARGE.") ";
			$_where .= " and user_id = ".$user_id;
			
			$_where .= " and add_time > ".$weekend;	
			
			$_order = " order by add_time asc ";
			$sql_result = "select sum(money) mons from {$table}";
			$sql_result .= $_where;
			//$sql_result .= $_order;
			
			$query = $this->db->query($sql_result);
			$dont_moeny = $query->row()->mons;
			if(!isset($dont_moeny)) $dont_moeny = 0;
			
			
			$table = 'rsh_account_cash';
			$_where = " where fstatus in (1,7,4) and user_id = ".$user_id." and add_time > ".$weekend;
			$sql_result = "select sum(fee) fess from {$table}";
			$sql_result .= $_where;
			$query = $this->db->query($sql_result);
			$fess = $query->row()->fess;
			if(!isset($fess)) $fess = 0;
			$money  =  $fess * 200;
			$money = round($money,2);
			if($money >= $dont_moeny ) $dont_moeny =0 ;
			else $dont_moeny = $dont_moeny - $money;
			return $dont_moeny;
		
		
	}
	
	function donnot_mention($user_id,$coupon_time){
		
			//1.查询某个用户有多少奖励
			
			$table = 'rsh_account_log';
			$mode = substr(strval($user_id),-1,1);
			$table = $table . "_" . $mode;

			$_where = " where 1=1 ";
			//$_where .= " and ftype in($invite_reward,$tender_reward) ";
			//推荐奖励 INVITE_REWARD
			$_where  .=" and ftype = ".INVITE_REWARD;
			$_where .= " and user_id = ".$user_id;
			$_where .= " and add_time > ".$coupon_time;
		
			$sql_result = "select sum(money) as count from {$table}";
			$sql_result .= $_where;
			
			$query = $this->db->query($sql_result);
			$coupon_money =  $query->row()->count;
			
			$dont_moeny = 0;
			if($coupon_money>0){
				//查奖励金额 有没有投资
				$sql = "SELECT SUM(p1.account) AS money	FROM rsh_borrow_tender AS p1 LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id 
		WHERE p1.add_time>=$coupon_time and user_id=".$user_id;
				$query = $this->db->query($sql);
				$tender_money = $query->row()->money;
				
				if($coupon_money <=$tender_money){
					 $time = time();
					 $this->db->query("update rsh_account set coupon_time={$time} where user_id={$user_id}");
				}else{
					$dont_moeny = $tender_money - $coupon_money;
				}
			}
			return $dont_moeny;
	}
	
	
}